package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.setting.eneity.User;
import com.bjpowernode.crm.util.DateTimeUtil;
import com.bjpowernode.crm.util.UUIDUtil;
import com.bjpowernode.crm.vo.PageinationVO;
import com.bjpowernode.crm.workbench.eneity.Activity;
import com.bjpowernode.crm.workbench.exception.ActivityException;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class ActivityController {
    @Resource
    private ActivityService activityService;
    @ResponseBody
    @RequestMapping(value = "/workbench/activity/add.do")
    public Map<String,Object> add(Activity activity, HttpServletRequest request){
        //获取请求参数,给参数未赋值的进行赋值
        activity.setId(UUIDUtil.getUUID());
        activity.setCreateTime(DateTimeUtil.getSysTime());
        HttpSession session=request.getSession(false);
        User user= (User) session.getAttribute("loginUser");
        activity.setCreateBy(user.getName());
        //声明map，将结果放入map
        Map<String,Object> map=new HashMap<>();
        boolean success;
        String msg;
        //调用service层
        try {
            activityService.add(activity);
            success=true;
            msg="添加成功";
            map.put("success",success);
            map.put("msg",msg);
        }catch (Exception ex){
            ex.printStackTrace();
            success=false;
            msg=ex.getMessage();
            map.put("success",success);
            map.put("msg",msg);
        }finally {
            return map;
        }
    }
    //分页查询的操作
    @ResponseBody
    @RequestMapping(value = "/workbench/activity/queryList.do")
    public PageinationVO<Activity> query(HttpServletRequest request){
        String pageNoStr=request.getParameter("pageNo");
        String pageSizeStr=request.getParameter("pageSize");
        String name=request.getParameter("name");
        String owner=request.getParameter("owner");
        String startDate=request.getParameter("startDate");
        String endDate=request.getParameter("endDate");
        Integer pageNo=null;
        Integer pageSize=Integer.valueOf(pageSizeStr);
        //判断页码参数是否存在
        if (pageNoStr==null||"".equals(pageNo)) {
            pageNo = 1;
        }else {
            pageNo=Integer.valueOf(pageNoStr);
        }
        //将参数封装成一个map
        Map<String,Object> map=new HashMap<>();
        map.put("name",name);
        map.put("owner",owner);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        //调用service层，进行查询
        PageinationVO<Activity> activityList= null;
        activityList = activityService.queryList(map,pageNo,pageSize);
        return activityList;
    }
    //删除市场活动信息
    @ResponseBody
    @RequestMapping(value = "/workbench/activity/delete.do")
    public Map<String,Boolean> deleteActivity(HttpServletRequest request){
        //获取请求参数
        String[] ids=request.getParameterValues("id");
        //调用service层
        Boolean success=activityService.delete(ids);
        Map<String,Boolean> map=new HashMap<>();
        map.put("success",success);
        return map;
    }
    @ResponseBody
    @RequestMapping(value = "/workbench/activity/selectById.do")
    public Activity selectById(String id){
        //调用service层
        Activity activity=activityService.selectById(id);
        return activity;
    }
    @ResponseBody
    @RequestMapping(value = "/workbench/activity/update.do")
    public Map<String,Boolean> updateActivity(HttpServletRequest request,Activity activity){
        //设置信息更新时间和更新人
        HttpSession session=request.getSession(false);
        User user= (User) session.getAttribute("loginUser");
        activity.setEditBy(user.getName());
        activity.setEditTime(DateTimeUtil.getSysTime());
       //调用service层
       Boolean success=activityService.updateById(activity);
       //将结果写入map
        Map<String, Boolean> map=new HashMap<>();
        map.put("success",success);
        return map;
    }
    @RequestMapping(value ="/workbench/activity/detail.do" )
    public ModelAndView queryActivityMess(String id){
        //调用service层进行查询
        Map<String,Object> map=activityService.queryById(id);
        //将查询结果写入请求作用域进行转发
        ModelAndView model=new ModelAndView();
        model.addObject("msg",map);
        model.setViewName("/workbench/activity/detail.jsp");
        return model;
    }
}
